




package com.yinhai.ta404.core.restservice;

import com.google.gson.Gson;
import com.yinhai.ta404.component.org.sysmg.resource.service.read.impl.ResourceReadServiceImpl;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.restservice.resultbean.ResultBean;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.utils.JsonFactory;
import com.yinhai.ta404.core.utils.ServiceLocator;
import com.yinhai.ta404.core.utils.ValidateUtil;
import com.yinhai.ta404.core.utils.WebUtil;
import java.util.Map;
import java.util.Objects;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

public class BaseRestService extends AdapterRestService implements IBaseRestService {

    private static final Logger logger = LoggerFactory.getLogger(BaseRestService.class);
    protected static final String RESULT = "result";
    protected static final String PAGE_BEAN = "pageBean";

    private static final String[] ADDR_HEADER = { "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP",
            "X-Real-IP" };
    private static final String NUKNOWN = "unknown";

    public BaseRestService() {
    }

    public ResultBean result() {
        ResultBean resultBean = (ResultBean)this.getRequest().getAttribute("_WEB_RESULT_BEAN");
        if (null == resultBean) {
            resultBean = (ResultBean)ServiceLocator.getAppContext().getBean(ResultBean.class);
            this.getRequest().setAttribute("_WEB_RESULT_BEAN", resultBean);
        }

        return resultBean;
    }

    public void setData(String fieldId, Object value) {
        ResultBean resultBean = this.result();
        resultBean.addField(fieldId, value);
        UserAccountVo userinfo = (UserAccountVo)this.getRequest().getSession().getAttribute("ta3.userinfo");
        Gson gson = new Gson();
        String myjson = gson.toJson(userinfo);
        logger.info(myjson);
        String ip = getRemoteAddr(this.getRequest());
        logger.info(ip);
    }

    public void setData(Map<String, Object> data, boolean clear) {
        ResultBean resultBean = this.result();
        resultBean.setData(data, clear);
    }

    public void setError(String errorMsg) {
        this.setSuccess(false);
        this.setError("418", errorMsg);
    }

    public void setError(String errorId, String errorMsg) {
        this.setSuccess(false);
        this.setObjErrors(errorId, errorMsg);
    }

    public void setErrors(String[] errorMsgs) {
        this.setSuccess(false);
        this.setErrors("418", errorMsgs);
    }

    public void setErrors(String errorId, String[] errorMsgs) {
        this.setObjErrors(errorId, errorMsgs);
    }

    public void setObjErrors(String errorId, Object errorMsgs) {
        ResultBean resultBean = this.result();
        if (null != errorId && null != errorMsgs) {
            if (errorMsgs instanceof String[]) {
                String[] var4 = (String[])((String[])errorMsgs);
                int var5 = var4.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    String errorMsg = var4[var6];
                    resultBean.addError(errorId, errorMsg);
                }
            } else {
                resultBean.addError(errorId, (String)errorMsgs);
            }
        }

    }

    public void setValidError(String errorCode, String parameter, String errorMsg) {
        ResultBean resultBean = this.result();
        resultBean.addError(errorCode, parameter, errorMsg);
    }

    public void setPageBean(Page<?> page) {
        this.result().addField("pageBean", page);
    }

    public void setSuccess(boolean success) {
        this.result().setSuccess(success);
    }

    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public HttpSession getSession() {
        return this.getRequest().getSession();
    }

    public HttpServletResponse getResponse() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
    }

    public MultipartHttpServletRequest getMultipartHttpServletRequest(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        return multipartResolver.isMultipart(request) ? (MultipartHttpServletRequest)request : null;
    }

    public void writeJsonToClient(Object obj) {
        throw new UnsupportedOperationException();
    }

    public String getCurUserId() {
        UserAccountVo userAccountVo = WebUtil.getCurUserAccountVo(this.getRequest());
        if (Objects.isNull(userAccountVo)) {
            return null;
        } else {
            return ValidateUtil.isEmpty(userAccountVo.getUserId()) ? null : userAccountVo.getUserId();
        }
    }

    public UserAccountVo getCurUserAccount() {
        UserAccountVo userAccountVo = WebUtil.getCurUserAccountVo(this.getRequest());
        return Objects.isNull(userAccountVo) ? new UserAccountVo() : userAccountVo;
    }


    public static String getRemoteAddr(ServletRequest request) {
        String addr = null;
        if (request instanceof HttpServletRequest) {
            HttpServletRequest hsr = (HttpServletRequest) request;
            for (String header : ADDR_HEADER) {
                if (StringUtils.isBlank(addr) || NUKNOWN.equalsIgnoreCase(addr)) {
                    addr = hsr.getHeader(header);
                } else {
                    break;
                }
            }
        }
        if (StringUtils.isBlank(addr) || NUKNOWN.equalsIgnoreCase(addr)) {
            addr = request.getRemoteAddr();
        } else {

            if (addr != null) {
                int i = addr.indexOf(",");
                if (i > 0) {
                    addr = addr.substring(0, i);
                }
            }

        }
        return addr;
    }
}
